﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System.Collections.Generic;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm;
using EntLibContrib.Resource.Properties;

namespace EntLibContrib.Resource.Configuration.Manageability
{
	/// <summary>
	/// Provides a default implementation for <see cref="CustomResourceManagerData"/> that
	/// splits policy overrides processing and WMI objects generation, performing appropriate logging of 
	/// policy processing errors.
	/// </summary>
	public sealed class CustomResourceManagerDataManageabilityProvider
		: CustomProviderDataManageabilityProvider<CustomResourceManagerData>
	{
		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="CustomResourceManagerDataManageabilityProvider"/> class.
		/// </summary>
		/// <remarks>Default to ReadOnly</remarks>
		public CustomResourceManagerDataManageabilityProvider()
			: base(InternalResources.ResourceManagerPolicyNameTemplate)
		{
			CustomResourceManagerDataWmiMapper.RegisterWmiTypes();
		}
		#endregion

		#region Public methods
		/// <summary>
		/// Adds the ADM instructions that describe the policies that can be used to override the properties of
		/// a specific instance of the configuration element type managed by the receiver.
		/// </summary>
		/// <param name="contentBuilder">The <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm.AdmContentBuilder"/> to which the Adm instructions are to be appended.</param>
		/// <param name="configurationObject">The configuration object instance.</param>
		/// <param name="configurationSource">The configuration source from where to get additional configuration
		/// information, if necessary.</param>
		/// <param name="parentKey">The key path for which the generated instructions' keys must be subKeys of.</param>
		/// <remarks>
		/// Class <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationElementManageabilityProviderBase`1"/> provides a default implementation for this method that
		/// calls the strongly typed
		/// <see cref="M:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationElementManageabilityProviderBase`1.AddAdministrativeTemplateDirectives(Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm.AdmContentBuilder,`0,Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationSource,System.String)"/>
		/// method.
		/// </remarks>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1061:DoNotHideBaseClassMethods")]
		internal new void AddAdministrativeTemplateDirectives(AdmContentBuilder contentBuilder,
																													ConfigurationElement configurationObject,
																													IConfigurationSource configurationSource,
																													string parentKey)
		{
			base.AddAdministrativeTemplateDirectives(contentBuilder,
																							configurationObject,
																							configurationSource,
																							parentKey);
		}

		/// <summary>
		/// Overrides the <paramref name="configurationObject"/>'s properties with the Group Policy values from the
		/// registry, if any, and creates the <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationSetting"/> instances that describe the
		/// configurationObject.
		/// </summary>
		/// <param name="configurationObject">The configuration object for instances that must be managed.</param>
		/// <param name="readGroupPolicies"><see langword="true"/> if Group Policy overrides must be applied; otherwise,
		/// <see langword="false"/>.</param>
		/// <param name="machineKey">The <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.IRegistryKey"/> which holds the Group Policy overrides for the
		/// configuration element at the machine level, or <see langword="null"/>
		/// if there is no such registry key.</param>
		/// <param name="userKey">The <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.IRegistryKey"/> which holds the Group Policy overrides for the
		/// configuration element at the user level, or <see langword="null"/>
		/// if there is no such registry key.</param>
		/// <param name="generateWmiObjects"><see langword="true"/> if WMI objects must be generated; otherwise,
		/// <see langword="false"/>.</param>
		/// <param name="wmiSettings">A collection to where the generated WMI objects are to be added.</param>
		/// <returns>
		/// 	<see langword="true"/> if the policy settings do not disable the configuration element, otherwise
		/// <see langword="false"/>.
		/// </returns>
		/// <exception cref="T:System.ArgumentException">when the type of <paramref name="configurationObject"/> is not
		/// the type <typeparamref name="T"/>.</exception>
		/// <remarks>
		/// Provides a default implementation that performs appropriate logging of errors when processing
		/// policy overrides.
		/// </remarks>
		/// <seealso cref="M:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationElementManageabilityProvider.OverrideWithGroupPoliciesAndGenerateWmiObjects(System.Configuration.ConfigurationElement,System.Boolean,Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.IRegistryKey,Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.IRegistryKey,System.Boolean,System.Collections.Generic.ICollection{Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationSetting})">ConfigurationElementManageabilityProvider.OverrideWithGroupPoliciesAndGenerateWmiObjects</seealso>
		/// <seealso cref="M:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationElementManageabilityProviderBase`1.OverrideWithGroupPolicies(`0,Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.IRegistryKey)"/>
		/// <seealso cref="M:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationElementManageabilityProviderBase`1.GenerateWmiObjects(`0,System.Collections.Generic.ICollection{Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationSetting})"/>
		internal new bool OverrideWithGroupPoliciesAndGenerateWmiObjects(ConfigurationElement configurationObject,
																																		bool readGroupPolicies,
																																		IRegistryKey machineKey,
																																		IRegistryKey userKey,
																																		bool generateWmiObjects,
																																		ICollection<ConfigurationSetting> wmiSettings)
		{
			return base.OverrideWithGroupPoliciesAndGenerateWmiObjects(configurationObject,
																																readGroupPolicies,
																																machineKey,
																																userKey,
																																generateWmiObjects,
																																wmiSettings);
		}
		#endregion

		#region Protected methods
		/// <summary>
		/// Creates the <see cref="ConfigurationSetting"/> instances that describe the
		/// configurationObject.
		/// </summary>
		/// <param name="configurationObject">The configuration object for instances that must be managed.</param>
		/// <param name="wmiSettings">A collection to where the generated WMI objects are to be added.</param>
		protected override void GenerateWmiObjects(CustomResourceManagerData configurationObject, ICollection<ConfigurationSetting> wmiSettings)
		{
			CustomResourceManagerDataWmiMapper.GenerateWmiObjects(configurationObject, wmiSettings);
		}
		#endregion
	}
}
